package asapIs134CK.womi.client.games.sudoku;

import asapIs134CK.womi.client.games.GameType;
import asapIs134CK.womi.client.games.GameWidget;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Sudoku extends GameWidget{
	private DockPanel panel;
	
	private FlowPanel toolbar;
	private Button newGame;
	private ClickHandler newGameClickHandler;
	private ListBox difficultyListBox;
	private Button hint;
	private ClickHandler hintClickHandler;
	private Button check;
	private ClickHandler checkClickHandler;
	private final int VERYEASY = 60;
	private final int EASY = 50;
	private final int NORMAL = 40;
	private final int HARD = 35;
	private final int VERYHARD = 30;
	
	private VerticalPanel left;
	
	private SudokuGame game;
	
	public Sudoku(){
		super();
		panel = new DockPanel();
		SudokuGenerator gen = new SudokuGenerator();
		gen.generatePuzzle(VERYEASY, 1);
		gen.solvePuzzle();
		
		game = new SudokuGame(gen.getGrid());
		toolbar = new FlowPanel();
		newGameClickHandler = new ClickHandler(){

			public void onClick(ClickEvent event) {
				newGame();
			}
		};
		newGame = new Button("<img src=\"imgs/PNG/newGame.png\" alt=\"\" /> New game", newGameClickHandler);
		hintClickHandler = new ClickHandler(){

			public void onClick(ClickEvent event) {	
				game.displayHint();
			}
		};
		difficultyListBox = new ListBox();
		difficultyListBox.addItem("Very easy", VERYEASY+"");
		difficultyListBox.addItem("Easy", EASY+"");
		difficultyListBox.addItem("Normal", NORMAL+"");
		difficultyListBox.addItem("Hard", HARD+"");
		difficultyListBox.addItem("Very hard", VERYHARD+"");
		hint = new Button("<img src=\"imgs/PNG/hint.png\" alt=\"\" /> Hint", hintClickHandler);
		checkClickHandler = new ClickHandler(){

			public void onClick(ClickEvent event) {
				if(game.isCorrect()){
					gameOver();
				}
				else{
					Window.alert("There are still some errors");
				}
			}
		};
		check = new Button("<img src=\"imgs/PNG/check.png\" alt=\"\" /> Check", checkClickHandler);
		toolbar.add(newGame);
		toolbar.add(difficultyListBox);
		toolbar.add(hint);
		toolbar.add(check);
		toolbar.addStyleName("ToolBar");
		panel.add(toolbar, DockPanel.NORTH);
		panel.add(game, DockPanel.CENTER);
		left = new VerticalPanel();
		left.add(new HTMLPanel("<div class=\"Information\">" +
									"<div><b>Rules of Sudoku</b></div>" +
									"<p>Sudoku is a very simple game. " +
									"All you have to do is fill out the " +
									"missing tiles of the grid with digits from 1 to 9. " +
									"Make sure that each digit appears only once in each row, " +
									"line and 3x3 box.</p>" +
									"<p><i>Note</i> : You can toggle between edition mode " +
									"and annotation mode by double clicking on a tile.</p>" +
							   "</div>"));
		panel.add(left, DockPanel.WEST);
		panel.addStyleName("Game");
		this.initWidget(panel);
	}

	protected void newGame() {
		Image loader = new Image("../../imgs/Sudoku/ajax-loader.gif");
		loader.setTitle("loading");
		panel.remove(game);
		panel.add(loader, DockPanel.CENTER);
		int diff = Integer.parseInt(difficultyListBox.getValue(difficultyListBox.getSelectedIndex()));
		SudokuGenerator gen = new SudokuGenerator();
		gen.generatePuzzle(diff, 2);
		gen.solvePuzzle();
		SudokuGame tmp = new SudokuGame(gen.getGrid());
		game = tmp;
		panel.remove(loader);
		panel.add(game, DockPanel.CENTER);
	}
	

	private void gameOver() {
		check.setEnabled(false);
		int score = game.getScore();
		this.submitScore(score, GameType.Sudoku);
		DialogBox dbEndOfGame = new DialogBox();
		dbEndOfGame.center();
		dbEndOfGame.add(new Label("You scored "+score+" points !"));
		dbEndOfGame.setAutoHideEnabled(true);
		dbEndOfGame.show();
	}
}
